unit Unit1;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, rtcDataCli, Vcl.StdCtrls, rtcInfo,
  rtcConn, rtcHttpCli, IdBaseComponent, IdComponent, IdTCPConnection,
  IdTCPClient, IdHTTP;

type
  TForm1 = class(TForm)
    RtcHttpClient1: TRtcHttpClient;
    Btnx_1: TButton;
    RtcDataRequest1: TRtcDataRequest;
    Btnx_2: TButton;
    Btnx_3: TButton;
    IdHTTP1: TIdHTTP;
    Btnx_4: TButton;
    Btnx_5: TButton;
    Btnv_1: TButton;
    procedure Btnx_1Click(Sender: TObject);
    procedure RtcDataRequest1BeginRequest(Sender: TRtcConnection);
    procedure RtcDataRequest1DataSent(Sender: TRtcConnection);
    procedure Btnx_2Click(Sender: TObject);
    procedure RtcDataRequest1DataReceived(Sender: TRtcConnection);
    procedure Btnx_3Click(Sender: TObject);
    procedure Btnx_4Click(Sender: TObject);
    procedure Btnx_5Click(Sender: TObject);
    procedure RtcHttpClient1ConnectError(Sender: TRtcConnection; E: Exception);
    procedure RtcHttpClient1ConnectFail(Sender: TRtcConnection);
    procedure Btnv_1Click(Sender: TObject);
  private
    FTargetName:string;
    FTargetPath:string;
  public
    procedure StartUpload;
    procedure StartObtain;
  end;

var
  Form1: TForm1;

implementation

uses
  Class_KzUtils,Class_KzDebug;


{$R *.dfm}

procedure TForm1.Btnv_1Click(Sender: TObject);
begin
  with RtcHttpClient1 do
  begin
    ServerAddr:='192.168.0.51';
    ServerPort:='9001';
    if isConnected then
    begin
      Disconnect;
    end;
    Connect();
  end;

  with RtcDataRequest1 do
  begin
    Request.FileName:='/helloworld?whobuild=1001';
    Request.Params['DATA']:='TRUE';
    Request.Method  :='POST';
    Post();
  end;
end;

procedure TForm1.Btnx_1Click(Sender: TObject);
var
  OD:TOpenDialog;
begin
  try
    OD:=TOpenDialog.Create(nil);
    if OD.Execute then
    begin
      FTargetPath:=OD.FileName;
      FTargetName:=ExtractFileName(FTargetPath);

      StartUpload;
    end;
  finally
    FreeAndNil(OD);
  end;
end;

procedure TForm1.Btnx_2Click(Sender: TObject);
begin
  StartObtain;
end;

procedure TForm1.Btnx_3Click(Sender: TObject);
begin
  FTargetName:='demo.doc';
  FTargetPath:=TKzUtils.ExePath+'OBTAIN\demo.doc';
  if not DirectoryExists(TKzUtils.ExePath+'OBTAIN') then
  begin
    CreateDir(TKzUtils.ExePath+'OBTAIN');
  end;

  with RtcHttpClient1 do
  begin
    ServerAddr:='192.168.0.51';
    ServerPort:='8080';
    if isConnected then
    begin
      Disconnect;
    end;
    Connect();
  end;

  with RtcDataRequest1 do
  begin
    Request.FileName:='FSFMP/uploadFiles/2016/2016022409383838593349190.doc';
    Request.Method  :='GET';
    Post();
  end;
end;

procedure TForm1.Btnx_4Click(Sender: TObject);
var
  Stream:TMemoryStream;
begin
  {#try
    Stream:=TMemoryStream.Create;
    IdHTTP1.Get('http://192.168.0.51:8080/FSFMP/uploadFiles/2016/2016022409383838593349190.doc',Stream);
    Stream.SaveToFile(TKzUtils.ExePath+'demoidhttp.doc');
  finally
    FreeAndNil(Stream);
  end;}

  try
    Stream:=TMemoryStream.Create;
    IdHTTP1.Get('http://192.168.0.51:8186/request',Stream);
    Stream.SaveToFile(TKzUtils.ExePath+'demoidhttp.doc');
  finally
    FreeAndNil(Stream);
  end;

end;

procedure TForm1.Btnx_5Click(Sender: TObject);
begin
  with RtcHttpClient1 do
  begin
    ServerAddr:='192.168.0.51';
    ServerPort:='8186';
    if isConnected then
    begin
      Disconnect;
    end;
    Connect();
  end;

  with RtcDataRequest1 do
  begin
    Request.FileName:='/request';
    Request.Method  :='GET';
    Post();
  end;
end;

procedure TForm1.RtcDataRequest1BeginRequest(Sender: TRtcConnection);
begin
  with TRtcDataClient(Sender) do
  begin
    if UpperCase(Request.FileName)='/UPLOAD' then
    begin
      WriteHeader;
    end else
    if UpperCase(Request.FileName)='/OBTAIN' then
    begin
      WriteHeader;
    end else
    begin
      Request.Host := ServerAddr+':'+ServerPort;
      Request.Agent :='Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.116 Safari/537.36';
      Request.HeaderText := 'Host:192.168.0.51:9001'+#13
                           +'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8'+#13
                           +'Accept-Encoding: identity'+#13
                           +'User-Agent: Mozilla/3.0 (compatible; Indy Library)';
      Write(Request.Params.Text);
    end;
  end;
end;

procedure TForm1.RtcDataRequest1DataReceived(Sender: TRtcConnection);
var
  Value:string;
begin
  with TRtcDataClient(Sender) do
  begin
    if (Request.FileName='/OBTAIN') then
    begin
      if Response.Started then
      begin
        Delete_File(FTargetPath);
      end;

      Value := Read;
      KzDebug.FileFmt('%d:%d',[Response.ContentIn - Length(Value),Length(Value)]);
      Write_File(FTargetPath,Value,Response.ContentIn - Length(Value));

      if Response.Done then
      begin
        Disconnect;
      end;
    end else
    if Pos('FSFMP',Request.FileName)>0 then
    begin
      if Response.Started then
      begin
        Delete_File(FTargetPath);
      end;

      Value := Read;
      KzDebug.FileFmt('%d:%d',[Response.ContentIn - Length(Value),Length(Value)]);
      Write_File(FTargetPath,Value,Response.ContentIn - Length(Value));

      if Response.Done then
      begin
        Disconnect;
      end;
    end else
    begin
      if Response.Done then
      begin
        ShowMessageFmt('%S',[Read]);
      end;
    end;
  end;
end;

procedure TForm1.RtcDataRequest1DataSent(Sender: TRtcConnection);
var
  bSize, j: Int64;
begin
  with TRtcDataClient(Sender) do
  begin
    if Request.ContentLength > Request.ContentOut then
    begin
      j := 1024 * 64;
      bSize := Request.ContentLength - Request.ContentOut;
      if bSize > j then
      begin
        bSize := j;
      end;
      Write(Read_File(FTargetPath,Request.ContentOut, bSize));
    end;
  end;
end;

procedure TForm1.RtcHttpClient1ConnectError(Sender: TRtcConnection;
  E: Exception);
begin
  ShowMessageFmt('RtcHttpClient1ConnectError:%S',[E.Message]);
end;

procedure TForm1.RtcHttpClient1ConnectFail(Sender: TRtcConnection);
begin
  ShowMessageFmt('%S',['RtcHttpClient1ConnectFail']);
end;

procedure TForm1.StartObtain;
begin
  FTargetName:='demo.exe';
  FTargetPath:=TKzUtils.ExePath+'OBTAIN\demo.exe';
  if not DirectoryExists(TKzUtils.ExePath+'OBTAIN') then
  begin
    CreateDir(TKzUtils.ExePath+'OBTAIN');
  end;

  with RtcHttpClient1 do
  begin
    ServerAddr:='localhost';
    ServerPort:='9090';
    if isConnected then
    begin
      Disconnect;
    end;
    Connect();
  end;

  with RtcDataRequest1 do
  begin
    Request.FileName:='/OBTAIN';
    Request.Method  :='POST';
    Request.ContentLength :=File_Size(FTargetPath);
    Request.Query['OBTAIN_TARGET'] := URL_Encode(FTargetName);
    Post();
  end;
end;

procedure TForm1.StartUpload;
begin
  with RtcHttpClient1 do
  begin
    ServerAddr:='localhost';
    ServerPort:='9090';
    if isConnected then
    begin
      Disconnect;
    end;
    Connect();
  end;

  with RtcDataRequest1 do
  begin
    Request.FileName:='/UPLOAD';
    Request.Method  :='POST';
    Request.ContentLength :=File_Size(FTargetPath);
    Request.Query['UPLOAD_TARGET'] := URL_Encode(FTargetName);
    Post();
  end;
end;

end.
